package com.woniu.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.project.core.domain.Image;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dao.ImageMapper;
import com.woniu.project.dto.imagedto.ImageDto;
import com.woniu.project.service.IImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.project.web.common.UploadImg;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WHF
 * @since 2020-10-13
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements IImageService {

    @Autowired
    private ImageMapper imageMapper;

    @Override
    public List<ImageDto> selectImageByProductEntityId(Integer productEntityId) {
        QueryWrapper<Image> imageQueryWrapper=new QueryWrapper<>();
        imageQueryWrapper.eq("product_entity_id",productEntityId);
        List<Image> images = imageMapper.selectList(imageQueryWrapper);
        List<ImageDto> imageDto=new ArrayList<>();
        BeanUtils.copyProperties(images,imageDto);
        return imageDto;
    }

    @Override
    public void deleteImages(Integer productEntityId) {
        imageMapper.deleteImages(productEntityId);
    }

    @Override
    public void insertImage(Integer productEntityId, String imageaddr, String imageComment) {
        Image image=new Image();
        image.setProductEntityId(productEntityId);
        image.setImageAddr(imageaddr);
        image.setImageComment(imageComment);
        imageMapper.insert(image);
    }

    @Override
    public String iconUpload(MultipartFile file) {
        try {
            String addr = UploadImg.upLoad(file);
            return addr;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500,"文件上传异常");
        }

    }
}
